import type { ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import useProTableConfig from '../../hooks/useProTableConfig';
import styles from './index.less';
// import type { FileRuleOTD } from '@/services/types/rulesCenter/modelManagement';
import { openEnum, sensitiveEnum } from '@/enum';
import { FileAddFilled } from '@ant-design/icons';
import { Button, Popconfirm, Space } from 'antd';
import { useEffect } from 'react';
import { history, useModel } from 'umi';

import hooks from '../../hooks';
export default () => {
  const {
    formRef,

    tabsType,
    tabKey,
    getCorpusBase,
    setKeyWord,
    setCorpusData,
    setCorpusVisible,
    deleteCorpusBaseData,
    setCreateKnowledgeVisible,
  } = hooks.useContainer();
  const proTableConfig = useProTableConfig({
    optionRender(config, props, dom) {
      return [...dom.reverse()];
    },
  });

  const { getRole } = useModel('useUser', (model) => {
    return {
      getRole: model.getRole,
    };
  });
  const { actionRef } = useModel('useTree', (model) => {
    return {
      actionRef: model.actionRef,
    };
  });

  useEffect(() => {
    return () => {
      setKeyWord('');
    };
  }, []);

  const columns: ProColumns<any>[] = [
    {
      dataIndex: 'index',
      valueType: 'indexBorder',
      width: 48,
      title: '序号',
    },
    {
      title: '语料库名称',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '是否高质量数据集',
      dataIndex: 'isHeightQuality',
      ellipsis: true,
      width: 146,
      renderText: (_, record) => {
        return <>{record.isHeightQuality ? '是' : '否'}</>;
      },
    },
    {
      title: '公开属性',
      dataIndex: 'openProperty',

      width: 126,
      hideInSearch: true,
      renderText: (_, record) => {
        return <>{openEnum[record.openProperty + '']}</>;
      },
    },
    {
      title: '敏感等级',
      width: 126,
      dataIndex: 'sensitiveLevel',
      hideInSearch: true,
      renderText: (_, record) => {
        return <>{sensitiveEnum[record.sensitiveLevel + '']}</>;
      },
    },
    // {
    //   title: '创建人',
    //   dataIndex: 'createBy',
    //   hideInSearch: true,
    // },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      ellipsis: true,
      hideInSearch: true,
    },

    {
      title: '更新时间',
      dataIndex: 'updateTime',
      hideInSearch: true,
      ellipsis: true,
    },
    {
      title: '描述',
      dataIndex: 'describe',
      width: 160,
      ellipsis: true,
      hideInSearch: true,
    },

    {
      title: '操作',
      width: 226,
      dataIndex: 'operate',
      hideInSearch: true,
      renderText: (text, record) => {
        return (
          <Space size="small" className={styles.spaceWrap}>
            <a
              onClick={() => {
                setCorpusData({ ...record });
                setCreateKnowledgeVisible(true);
              }}
            >
              生成知识库
            </a>
            <a
              onClick={() => {
                history.push(
                  {
                    pathname: '/resourceManagement/corpus-file',
                  },
                  {
                    resourceId: record.id,
                    openProperty: record.openProperty,
                    sensitiveLevel: record.sensitiveLevel,
                  },
                );
              }}
            >
              详情
            </a>
            <a
              onClick={() => {
                setCorpusData({ ...record });
                setCorpusVisible(true);
              }}
            >
              修改
            </a>

            <Popconfirm
              key="delete"
              title="确认是否删除？"
              onConfirm={() => {
                deleteCorpusBaseData.run(record?.id);
              }}
            >
              <a style={{ color: '#f02323' }}>删除</a>
            </Popconfirm>
          </Space>
        );
      },
    },
  ];
  return (
    <>
      <ProTable
        {...proTableConfig}
        className={styles.resourceTableWrap}
        actionRef={actionRef}
        formRef={formRef}
        columns={columns}
        request={getCorpusBase.runAsync}
        size="middle"
        rowKey="id"
        ghost={true}
        search={false}
        // options={{
        //   search: true,
        // }}
        toolbar={{
          actions: [
            <Button
              key="create"
              icon={<FileAddFilled />}
              type="primary"
              onClick={() => {
                setCorpusVisible(true);
              }}
            >
              {`创建${tabsType[tabKey].name}`}
            </Button>,
          ],
          search: {
            style: {
              width: '300px',
            },
            placeholder: '请输入内容',
            onSearch: (arg) => {
              console.log(arg);
              setKeyWord(arg);
              actionRef.current?.reload();
            },
          },
        }}
      />
    </>
  );
};
